package com.imy.common.view.recyclerview.adpater;

import android.app.Activity;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.imy.common.view.recyclerview.adapterdelegates.AdapterDelegate;
import com.imy.common.view.recyclerview.adapterdelegates.AdapterDelegatesManager;

import java.util.List;

/**
 * 功能描述：多布局（不含有加载更多视图，如有需要，继承BaseLoadMoreAdapter）
 *
 * @author 黄一凡
 * @date 2018/8/13 0013
 */

public class BaseMultipleAdapter<T> extends RecyclerView.Adapter {
  private AdapterDelegatesManager<List<T>> delegatesManager;
  private List<T> items;

  public BaseMultipleAdapter(Activity activity, List<T> items) {
    this.items = items;
    // Delegates
    delegatesManager = new AdapterDelegatesManager<>();
  }

  public void setItems(List<T> items) {
    this.items = items;
  }

  public List<T> getItems() {
    return items;
  }

  public void addDelegate(AdapterDelegate adapterDelegate) {
    if (delegatesManager == null) {
      throw new NullPointerException("AdapterDelegatesManager is null!");
    }
    delegatesManager.addDelegate(adapterDelegate);
  }

  @Override
  public int getItemViewType(int position) {
    return delegatesManager.getItemViewType(items, position);
  }

  @NonNull
  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    return delegatesManager.onCreateViewHolder(parent, viewType);
  }

  @Override
  public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    delegatesManager.onBindViewHolder(items, position, holder);
  }

  @Override
  public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
    delegatesManager.onViewAttachedToWindow(holder);
  }

  @Override
  public void onViewRecycled(RecyclerView.ViewHolder holder) {
    delegatesManager.onViewRecycled(holder);
  }

  @Override
  public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
    delegatesManager.onViewDetachedFromWindow(holder);
  }

  @Override
  public int getItemCount() {
    return items == null ? 0 : items.size();
  }
}
